package com.example.productserver.controller;

import com.example.productserver.entity.Product;
import com.example.productserver.service.IProductService;
import com.example.productserver.service.IProductTccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author whx
 * @date 2022/4/3
 */
@RestController
@AllArgsConstructor
@Api(value = "商品Controller", tags = "商品接口")
public class ProductController {
    private final IProductService productService;

    private final IProductTccService productTccService;

    @ApiOperation(value = "列表", notes = "无参数")
    @GetMapping("list")
    public List<Product> list(){
        return productService.list();
    }

    @ApiOperation(value = "保存", notes = "无参数")
    @PostMapping("save")
    public String save(){
        Product product = new Product();
        product.setId(555L);
        product.setName("商品xxx");
        product.setPrice(300.0);
        product.setNo("SP001");
        product.setCreateTime(new Date());
        int i = 1/0;
        return productService.save(product) ? "保存成功" : "保存失败";
    }

    @ApiOperation(value = "库存扣减", notes = "传入商品ID，扣减数量")
    @PostMapping("reduceInventory")
    public String reduceInventory(Long productId,Integer number){
        return productTccService.reduceInventory(productId,number);
    }
}
